package cn.itcast.controller;

import cn.itcast.bean.Member;
import cn.itcast.service.MemberService;
import cn.itcast.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("member")
@CrossOrigin
public class MemberController {

    @Autowired
    MemberService memberService;
    @GetMapping("list")
    public Result getlist(){
        List<Member> list=memberService.list();
        return Result.success(list);
    }

    @GetMapping("page")
    public Result getPage(Page page)
    {
        IPage result=memberService.page(page);
        return Result.success(result);
    }

    @PostMapping("add")
    public Result add(@RequestBody Member member){
        memberService.save(member);
        Member savemember=memberService.getById(member.getId());
        return Result.success("保存成功",savemember);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id)
    {
        boolean b =memberService.removeById(id);
        if (b){
            return Result.success("删除成功");

        }
        else {
            return Result.fail(201,"删除失败");
        }
    }
    @PostMapping("edit")
    public Result edit(@RequestBody Member member)
    {
        memberService.saveOrUpdate(member);
        boolean b=memberService.saveOrUpdate(member);
        Member member1=memberService.getById(member.getId());
        if(b){
            return Result.success("保存成功",member1);
        }
        else{
            return Result.fail(201,"修改失败");
        }
    }
}
